Regexp Help...resolving a textual list of identifiers, such as SRS-1,8,10

I am attempting to parse references to requirements embedded in Object Text.

I have 4 cases that I need to cater for:

1. Isolated requirement reference, e.g "SRS-1" outputs "SRS-1"

2. Requirement reference range type 1, e.g. "SRS-1 to 3" outputs "SRS-1, SRS-2, SRS-3"

3. Requirement reference range type 2, e.g. "SRS-1 to SRS-3" outputs "SRS-1, SRS-2, SRS-3"

4. Requirement reference list, e.g. "SRS-1,8,10" outputs "SRS-1, SRS-8, SRS-10"

I have written regexps and associated code for cases 1, 2 and 3, but am having difficulty in defining a regexp and code for case 4.

Is there a regexp guru that can help?


Antonio__Norkus - Thu May 14 09:49:06 EDT 2015

Re: Regexp Help...resolving a textual list of identifiers, such as SRS-1,8,10
Mathias Mamsch - Mon May 25 05:03:55 EDT 2015

Well you will probably not solve that with one regexp, but rather with a couple:

PREFIX = "[-a-zA-Z_]"   // prefix contains letters, underscore or minus

IDENTIFIER = PREFIX "-[1-9][0-9]*"    // identifier is a prefix followed by minus followed by a number (not starting with 0)

WHITE = "[ ]+"   // whitespace are one or more spaces

RANGESTART= "^" IDENTIFIER WHITE "to" IDENTIFIER

LISTSTART="^" IDENTIFIER ","   // No whitespace allowed before the commas in the list?`

Once you defined all your regexps, you can do some DXL like this:

int p0 = 0; // we start searching here 
Buffer buf = ... // the buffer you want to search 


if (search (IDENTIFIER, buf, p0 + 0)) {
    int iStart = p0 + start 0; 
    int iEnd   = p0 + end 0; 

    // check for start of range
    if (search (RANGESTART, buf, iStart)) {
           // we got a range, get the identifiers and start processing 
    } 

    else if (search (LISTSTART, buf, iStart)) {
          // we got the start of a list
           
          ... // parse the identifiers, starting from iEnd
    } 
    
    else {
         // a simple identifier
    }
}   

Hope that gives you a start. Regards, Mathias